var assert = require('node-assert-extras');
var mSession = require(__testRoot + '/model/SessionEntity');
/**
 * Session Entity Testing
 * @author jmanero
 */
suite("Session Entity", function() {
	var session = {};
	
	test("Create new SessionEntity", function() {
		var testId = "123456";
		var before = Date.now();
		
		session = new mSession(testId);
		
		assert.equal(session.id, testId, "Wrong session ID!");
		assert.isNotNull(session.socket, "No Socket");
		assert.ok(session.lastAccess >= before, "Invalid Timestamp");
		
	});
	
	test("Send a message to the DLServer", function(done) {
		this.timeout(100);
		session.toServer({
			__XMLclass : "request",
			id : "asdfasdf",
			version : "1.0",
			__XMLchildren : [{
				__XMLclass : "createRequest",
				things : "All of them"
			}]
		});
		
		setTimeout(function() {
			assert.ok(session.serverBuffer.length >= 1, "ServerBuffer error: " + session.serverBuffer.length);
			done();
		}, 50);
	});
	
	test("Receive asynchronous messages from DLServer", function(done) {
		session.socket.on('data', function() {
			assert.ok(session.clientBuffer.length >= 1, "ClientBuffer error");
		});
		
		setTimeout(function() {
			done();
		}, 1000);
	});
	
	test("Connect to DLServer", function(done) {
		this.timeout(500);
		
		session.socket.on('connect', function() {
			setTimeout(function() { // Wait for a response to be buffered
				assert.ok(session.serverConnected, "Invalid 'ServerConnected' flag");
				assert.equal(session.serverBuffer.length, 0, "Server Buffer not empty after connect");
				
				done();
			}, 50);
		});
		
		session.socket.on('error', function(e) {
			assert.ifError(e);
			done();
		});
		
		setTimeout(function() {
			session.start(9371, '127.0.0.1');
		}, 100);
	});
});
